package com.ccc.ka.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.ccc.ka.util.KAResponse;
import org.springframework.web.bind.annotation.*;
import com.ccc.ka.service.IUserService;
import com.ccc.ka.entity.User;
import com.ccc.ka.util.BaseController;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 
 * @author ccc
 * @since 2023-02-28
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

@Autowired
private IUserService userService;

@PostMapping("/login")
public KAResponse login(User user){
    //查找，用户名、手机号、邮箱、昵称作为名
    user.setPassword(MD5.create().digestHex16(user.getName() + user.getPassword()));
    user.setCrtTm(LocalDateTime.now());

    return success("登录成功");
}

@PostMapping("/saveUser")
public KAResponse saveUser(User user){

    user.setPassword(MD5.create().digestHex16(user.getName() + user.getPassword()));
    user.setCrtTm(LocalDateTime.now());

    return success(userService.save(user));
}

@DeleteMapping("/delUser")
public KAResponse delUser(User user){
    return success(userService.removeById(user));
}

@PutMapping("/updUser")
public KAResponse putUser(User user){
    user.setCrtTm(LocalDateTime.now());
    return success(userService.updateById(user));
}

@GetMapping("/getUser")
public KAResponse getUser(String id){
    return success(userService.getById(id));
}

@GetMapping("/getUserList")
public KAResponse getUserList(User user){
    return success(userService.list());
}

@GetMapping("/getUserPage")
public KAResponse getUserPage(User user){
    return success(userService.page(new Page<>(1,3)));
}

}
